//
//  FeedBackViewController.m
//  SinaRadio1.1.0
//
//  Created by 武 晓方 on 12-1-17.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "FeedBackViewController.h"

#import "NSString+Extension.h"

@implementation FeedBackViewController



- (void)dealloc
{
    [_tipView hide];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableString* tempString = [[NSMutableString alloc] init];
    _textView.text= tempString;
    [_textCountLabel setText:[NSString stringWithFormat:@"%d",shareTextCount - [_textView.text length]]];

    [_textCountLabel setText:[NSString stringWithFormat:@"%d",shareTextCount - [_textView.text length]]];
    _textView.selectedRange = NSMakeRange(0, 0);
    [tempString release];
    tempString = nil;


}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [_tipView hide];
    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self];
   }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}


- (void) initNavigationBar
{
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
    [tempLabel setText:@"意见反馈"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;

    
    UIImage  *backImage  = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 60, 30);
	[leftButton setBackgroundImage:backImage forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(cancelFeedBackButtonPress) forControlEvents:UIControlEventTouchUpInside];
        
    
    UIBarButtonItem*  leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //[leftButton release];     
    //leftButton     = nil;
    self.navigationItem.leftBarButtonItem = leftButtonItem;    
    [leftButtonItem release];
    leftButtonItem = nil;
    
    
    UIImage  *registImage = [UIImage imageNamed:@"btn_finish_normal.png"];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 50, 30);

	[rightButton setBackgroundImage:registImage forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"btn_finish_press.png"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(feedBackButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*  rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;    
    [rightButtonItem release];
    rightButtonItem = nil;
    
}
// user action
- (void) cancelFeedBackButtonPress
{
    [_tipView hide];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) feedBackButtonPress
{
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
	NSString *textString = _textView.text;
	textString= [textString stringByTrimmingCharactersInSet:whitespace];
    if([textString length] >0)
    {
        _tipView = [FXTipView showWaitTipWithTextAndIndicator:@"请稍等..."];
        //发送联网请求
        NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:textString/*@"martin.szj@gmail.com"*/,@"content",nil];
        CoreMsg* msg = [[CoreMsg alloc] init];
        msg.data = FEEDBACK;
        msg.ui_delegate = self;
        
        [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=do_member_idea" postInfo:info userInfo:msg];
    }
    else
    {
        [FXTipView showTipViewWithText:@"反馈内容不能为空"];
    }

}

#pragma -mark DataSourceDelegate

- (void)onDataSourceSucCallback:(CoreMsg *)coreMsg withData:(id)data
{
    [_tipView hide];
    if ([coreMsg.data isEqual: FEEDBACK]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"谢谢您的意见！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 2001;
        [alert show];
        [alert release];
    }
}

- (void)onDataSourceFailCallback:(CoreMsg *)coreMsg withData:(id)data
{
    [_tipView hide];
    if ([coreMsg.data isEqual: FEEDBACK]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:data delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 2001;
        [alert show];
        [alert release];
    }
}

@end
